/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package enterprise.web_jpa_war.servlet;

import enterprise.web_jpa_war.entity.Company;
import enterprise.web_jpa_war.entity.Customer;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import javax.persistence.PersistenceUnit;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;

/**
 *
 * @author GOSTEP
 */
@WebServlet(name="CreateCustomerServlet", urlPatterns={"/CreateCustomer.do"})
public class CreateCustomerServlet extends HttpServlet {
     @PersistenceUnit
    //The emf corresponding to
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction utx;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CreateCustomerServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CreateCustomerServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
               assert emf != null;  //Make sure injection went through correctly.
        EntityManager em = null;
        try {

            //Get the data from user's form
            String givenname = request.getParameter("givenname");
            String surname  = request.getParameter("surname");
            String phoneno  = request.getParameter("phoneno");
            String email  = request.getParameter("email");
            String company  = request.getParameter("company");

            utx.begin();
            //create an em.
            //Since the em is created inside a transaction, it is associsated with
            //the transaction
            em = emf.createEntityManager();

            Company s= em.find(Company.class, company);
            //Create a person instance out of it

            Customer customer = new Customer( givenname, surname, phoneno,email,s);
            //persist the person entity
            em.persist(customer);
            //commit transaction which will trigger the em to
            //commit newly created entity into database
            utx.commit();


           request.setAttribute("message","You have created a new Customer...");
           request.getRequestDispatcher("message.jsp").forward(request, response);
        } catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            //close the em to release any resources held up by the persistebce provider
            if(em != null) {
                em.close();
            }
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
